#include <iostream>
#include "LinkStack.h"

//表达式求值
//如：(1 + 2) * 3

/*判断是否是运算符*/
bool IsOper(char c)
{
    switch(c)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '(':
    case ')':
    case '=':
        return true;
    default:
        return false;
    }
}

/*判断是否是数字*/
bool IsNum(char c)
{
    if(c >= '0' && c <= '9')
    {
        return true;
    }
    return false;
}

int Operation(int prev, int next, char symbol)
{
    switch(symbol)
    {
    case '+': return prev + next; break;
    case '-': return prev - next; break;
    case '*': return prev * next; break;
    case '/': 
        if(next == 0)
        {
            printf("除以0");
            /*退出程序的函数*/
            exit (-1);
        }
        return prev / next; break;
    default:
        exit (-1);
    }
}

char PreCede(char ch1, char ch2)
{
    int row = 0, column = 0;
    switch (ch1)
    {
    case '+':row = 0;break;
    case '-':row = 1;break;
    case '*':row = 2;break;
    case '/':row = 3;break;
    case '(':row = 4;break;
    case ')':row = 5;break;
    case '=':row = 6;break;
    default:
        break;
    }

    switch (ch2)
    {
    case '+':column = 0;break;
    case '-':column = 1;break;
    case '*':column = 2;break;
    case '/':column = 3;break;
    case '(':column = 4;break;
    case ')':column = 5;break;
    case '=':column = 6;break;
    default:
        break;
    }

    char pre[7][7] = 
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=','0'},
        {'>','>','>','>','0','>','>'},
        {'<','<','<','<','<','0','='}
    };
    return pre[row][column];
}

int main()
{
    //数据栈
    LinkStack NumS;
    //运算符栈
    LinkStack OperS;
    OperS.Push('=');

    char str[64] = {0};
    scanf("%[^\n]", str);

    int i = 0;
    while(str[i] != '\0')
    {
        /*遇到数字*/
        if(IsNum(str[i]))
        {
            // /*自己代表的数字*/
            // int num = str[i] - '0';
            // i++;
            int num = 0;
            while(IsNum(str[i]))
            {
                num = 10 * num + str[i] -'0';
                i++;
            }
            std::cout<<"num: "<<num<<std::endl;
            NumS.Push(num);
        }
        else if(IsOper(str[i]))
        {
            char pre = PreCede(OperS.GetTop(),str[i]);
            switch(pre)
            {
                case '>':
                {
                    int next = NumS.GetTop();
                    NumS.Pop();
                    int prev = NumS.GetTop();
                    NumS.Pop();
                    char op = OperS.GetTop();
                    OperS.Pop();
                    NumS.Push(Operation(prev, next, op));
                    break;
                }
                case '<':
                {
                    OperS.Push(str[i]);
                    i++;
                    break;
                    
                }
                case '=':
                {
                    OperS.Pop();
                    i++;
                    break;
                }
                default:
                {
                    std::cout<<"表达式有误\n";
                    exit(-1);
                    break;
                }      
            }
        }
        else if(str[i] == ' ')
        {
            while(str[i] == ' ')
                i++;
        }
    }
    std::cout<<"答案是："<<NumS.GetTop()<<std::endl;
    return 0;
}